<?php

namespace app\common\model;

use think\Model;

class Newscat extends Base
{
    protected $beforeActionList = [

        'delsoncate'  =>  ['only'=>'del'],
    ];
    public function newscat(){
        return $this->belongsTo('Newscat')->bind('cate_name');
    }
    public function catetree()
    {
        $cateres=$this->order('displayorder desc')->select();
        return $this->sort($cateres);
    }
    public function sort($data,$pid=0,$level=0){
        static $arr=array();
        foreach ($data as $k => $v) {
            if($v['parent_id']==$pid){
                $v['level']=$level;
                $arr[]=$v;
                $this->sort($data,$v['catid'],$level+1);
            }
        }
        return $arr;
    }
    public function getchilrenid($cateid){
        $cateres=$this->select();
        return $this->_getchilrenid($cateres,$cateid);
    }

    public function _getchilrenid($cateres,$cateid){
        static $arr=array();
        foreach ($cateres as $k => $v) {
            if($v['parent_id'] == $cateid){
                $arr[]=$v['catid'];
                $this->_getchilrenid($cateres,$v['catid']);
            }
        }

        return $arr;
    }
}
